package com.fashion.service;

import com.fashion.dao.TagWordDao;
import com.fashion.exceptions.HtmlFileException;
import com.fashion.model.TagWord;
import com.fashion.utils.StringUtils;
import org.jsoup.Jsoup;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: gnyk
 * Date: 13-4-17
 */
@Service("tagWordServiceImpl")
public class TagWordServiceImpl implements TagWordService {
    private TagWordDao tagWordDao;

    @Override
    public List<TagWord> searchTagWords(String name, int pageNumber, int pageSize) {
        StringBuilder hql = new StringBuilder("from TagWord i ");
        Map<String, Object> parameters = new HashMap<String, Object>();
        if (!StringUtils.isNullOrEmpty(name)) {
            hql.append(" where i.word like :name ");
            parameters.put("name", "%" + name + "%");
        }

        hql.append(" order by i.createDate desc");

        return tagWordDao.findItemsWithPage(hql.toString(), parameters, pageNumber, pageSize);
    }

    @Override
    public int countTagWords(String name) {
        StringBuilder hql = new StringBuilder("select count(i.id) from TagWord i ");
        Map<String, Object> parameters = new HashMap<String, Object>();
        if (!StringUtils.isNullOrEmpty(name)) {
            hql.append(" where i.word like :name ");
            parameters.put("name", "%" + name + "%");
        }

        return tagWordDao.countItems(hql.toString(), parameters);
    }

    @Override
    public void encodeWithGB(List<TagWord> tagWordList) {
        for (TagWord tagWord : tagWordList) {
            String keyword = tagWord.getWord();
            tagWord.setUrlKeyword(StringUtils.makeUrlKeyword(keyword));
        }
    }

    @Override()
    public void refreshTagWordHtml(String persistenceFolder, String url) throws Exception {
        String fileName = persistenceFolder + File.separator + StringUtils.TagCloudHtmlName;
        File htmlFile = new File(fileName);
        if (htmlFile.exists()) {
            int count = 0;

            while (!htmlFile.delete() && count < 6) {
                Thread.sleep(6);
            }

            if (count > 5) {
                throw new HtmlFileException("Can not delete html file: " + StringUtils.TagCloudHtmlName);
            }

        }

        Jsoup.parse(new URL(url), 30 * 1000);
    }

    @Override
    public List<TagWord> loadAllWords() {
        return tagWordDao.find("from TagWord");
    }

    @Override
    public void saveOrUpdate(Object object) {
        tagWordDao.saveOrUpdate(object);
    }

    @Override
    public void deleteEntityById(String id) {
        tagWordDao.deleteById(id, TagWord.class);
    }

    @Override
    public void deleteEntity(Object object) {
        tagWordDao.delete(object);
    }

    @Resource(name = "tagWordDao")
    public void setTagWordDao(TagWordDao tagWordDao) {
        this.tagWordDao = tagWordDao;
    }
}
